栈和队列——猫狗队列

题目:定义了一个Pet类,Dog类和Cat类分别实现了Pet类。
实现一种猫狗队列:
调用add方法将Dog或Cat的实例放入队列中;
pollAll方法,把所有实例弹出;
pollDog方法,把Dog类实例弹出;
pollCat方法,把Cat类的实例弹出;
isEmpty方法
isDogEmpty方法
isCatEmpty方法

实现:将不同的类盖上时间戳,但又不能改变用户本身的类。
定义一个新的类PetEnterQueue,Pet pet是用户实例,int count为这个实例的时间戳。对于主类CatDogQueue,同时有两个队列,一个队列dogQ只放Dog类的实例,另一个队列catQ只放Cat类的实例;有一个不断累加的数据项count,用来表示实例进队列的时间。

1
2
3
4
5
6
7
8
9
10
package PetEnterQueue;
public class Pet {
private String type;
public Pet(String type) {
this.type = type;
}
public String getType(){
return this.type;
}
}
1
2
3
4
5
6
package PetEnterQueue;
public class Dog extends Pet {
public Dog(String type) {
super("dog");
}
}
1
2
3
4
5
6
package PetEnterQueue;
public class Cat extends Pet{
public Cat(String type) {
super("cat");
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package PetEnterQueue;
/*
* 定义一个类,将不同实例盖上时间戳
*/
public class PetEnterQueue {
private Pet pet;
private long count;
public PetEnterQueue(Pet pet, long count){
this.pet = pet;
this.count = count;
}
public Pet getPet() {
return this.pet;
}
public long getCount(){
return this.count;
}
public String getPetType() {
return this.pet.getType();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package PetEnterQueue;
import java.util.LinkedList;
import java.util.Queue;
/*
* 主类,定义两个队列,一个队列dogQ用于存储Dog类实例,
* 一个队里catQ用于存储Cat类实例。一个count计算时间戳
* Queue<PetEnterQueue> dogQ = new LinkedList<>();
*/
public class DogCatQueue {
private Queue<PetEnterQueue> dogQ;
private Queue<PetEnterQueue> catQ;
private long count;
public DogCatQueue() {
this.dogQ = new LinkedList<PetEnterQueue>();
this.catQ = new LinkedList<PetEnterQueue>();
this.count = 0;
}
/*
* 增加Pet实例到DogCatQueue队列,
* Dog类和Cat类的实例应该分别存在dogQ和catQ,同时计算时间戳count
*/
public void add(Pet pet) {
//long count = 0;
if (pet.getType().equals("dog")) {
dogQ.add(new PetEnterQueue(pet, count++)); //注意猫狗队列的类型Queue<PetEnterQueue>
} else if (pet.getType().equals("cat")) {
catQ.add(new PetEnterQueue(pet, count++));
} else {
System.out.println("err, not dog or cat");
}
}
/**
* 删除队列中的所有元素
* @return 这个删除的实例对象
*/
public Pet pollAll() {
if (!dogQ.isEmpty() && !catQ.isEmpty()) {
//如果dogQ队头的时间戳小于catQ的时间戳(即在猫狗队列类实例中,dogQ在前),则删除dogQ的队头
if (dogQ.peek().getCount() < catQ.peek().getCount()) {
return dogQ.poll().getPet();
} else {
return catQ.poll().getPet();
}
} else if (!dogQ.isEmpty()){
return dogQ.poll().getPet();
} else if (!catQ.isEmpty()) {
return catQ.poll().getPet();
} else {
throw new RuntimeException("err, queue is empty");
//如果用下面的输出错误,因为没有返回值,报错。
//System.out.println("err, queue is empty");
}
}
//删除dogQ中的所有元素
public Pet pollDog () {
if (!dogQ.isEmpty()) {
return dogQ.poll().getPet();
} else {
throw new RuntimeException("err, dog queue is empty");
}
}
//删除catQ中的所有元素
public Pet pollCat () {
if (!catQ.isEmpty()) {
return catQ.poll().getPet();
} else {
throw new RuntimeException("err, cat queue is empty");
}
}
//判断队列是否为空
public boolean isEmpty() {
return dogQ.isEmpty() && catQ.isEmpty();
}
//判断dogQ是否为空
public boolean isDogEmpty() {
return dogQ.isEmpty();
}
//判断catQ是否为空
public boolean isCatEmpty() {
return catQ.isEmpty();
}
}